package yi.Note.data.provider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

/**
 * Created with IntelliJ IDEA.
 * User: b251
 * Date: 13-12-7
 * Time: 下午7:52
 * To change this template use File | Settings | File Templates.
 */
public class NoteProvider extends ContentProvider {
    private SQLiteOpenHelper mOpenHelper;
    private static final int NOTE  = 10;
    private static final int NOTE_ID  = 11;
    private static final int NOTE_CLEAR  = 12;
    private static final int NOTE_ATT  = 20;
    private static final int NOTE_ATT_ID  = 21;
    private static final int NOTE_ATTS  = 22;

    private static final UriMatcher sURLMatcher = new UriMatcher(UriMatcher.NO_MATCH);

    static {
        sURLMatcher.addURI(NoteContent.AUTHORITY, NoteContent.Note.CONTENT_DIR,
                NOTE);
        sURLMatcher.addURI(NoteContent.AUTHORITY, NoteContent.Note.CONTENT_DIR
                + "/#", NOTE_ID);
        sURLMatcher.addURI(NoteContent.AUTHORITY, NoteContent.Note.CONTENT_DIR
                +"/clear",  NOTE_CLEAR);
        sURLMatcher.addURI(NoteContent.AUTHORITY, NoteContent.Attachment.CONTENT_DIR,
                NOTE_ATT);
        sURLMatcher.addURI(NoteContent.AUTHORITY, NoteContent.Attachment.CONTENT_DIR
                + "/#", NOTE_ATT_ID);
        sURLMatcher.addURI(NoteContent.AUTHORITY, NoteContent.Attachment.CONTENT_DIR
                + "/note/#", NOTE_ATTS);
    }

    @Override
    public boolean onCreate() {
        mOpenHelper = new NoteProviderHelper(getContext());
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();

        // Generate the body of the query.
        int match = sURLMatcher.match(uri);
        switch (match) {
            case NOTE:
                qb.setTables(NoteContent.TABLE_NOTE);
                break;
            case NOTE_ID:
                qb.setTables(NoteContent.TABLE_NOTE);
                qb.appendWhere(NoteContent.Note._ID + " = " + uri.getPathSegments().get(1));
                break;
            case NOTE_ATT:
                qb.setTables(NoteContent.TABLE_ATT);
                break;
            case NOTE_ATT_ID:
                qb.setTables(NoteContent.TABLE_ATT);
                qb.appendWhere(NoteContent.Attachment._ID + " = " + uri.getPathSegments().get(1));
                break;
            case NOTE_ATTS:
                qb.setTables(NoteContent.TABLE_ATT);
                qb.appendWhere(NoteContent.Attachment.NOTE_ID + " = " + uri.getPathSegments().get(1));
                break;
            default:
                throw new UnsupportedOperationException();
        }
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        Cursor cursor = qb.query(db, projection, selection, selectionArgs, null, null, sortOrder);
        cursor.setNotificationUri(getContext().getContentResolver(), uri);
        return cursor;
    }

    @Override
    public String getType(Uri uri) {
        int match = sURLMatcher.match(uri);
        switch (match) {
            case NOTE:
                return NoteContent.CONTENT_TYPE;
            case NOTE_ID:
                return NoteContent.CONTENT_ITEM_TYPE;
            default:
                throw new UnsupportedOperationException();
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues values) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowID;
        int match = sURLMatcher.match(uri);
        switch (match) {
            case NOTE:
                rowID = db.insert(NoteContent.TABLE_NOTE, null, values);
                getContext().getContentResolver().notifyChange(uri, null);
                if (rowID > 0) {
                    return Uri.parse(NoteContent.Note.CONTENT_URI + "/" + rowID);
                } else {
                    return null;
                }
            case NOTE_ATT:
                rowID = db.insert(NoteContent.TABLE_ATT, null, values);
                getContext().getContentResolver().notifyChange(uri, null);
                if (rowID > 0) {
                    return Uri.parse(NoteContent.Attachment.CONTENT_URI + "/" + rowID);
                } else {
                    return null;
                }
            default:
                throw new UnsupportedOperationException();
        }
    }

    @Override
    public int delete(Uri uri, String selection, String[] selectionArgs) {
        int count = 0;
        int match = sURLMatcher.match(uri);
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        switch (match) {
            case NOTE_CLEAR:
                count = db.delete(NoteContent.TABLE_NOTE, null, null);
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            case NOTE_ID:
                if (selectionArgs != null) {
                    throw new IllegalArgumentException(
                            "can not use selectionArgs when use profile_id");
                }
                count = db.delete(NoteContent.TABLE_NOTE, NoteContent.Note._ID + "="
                    + uri.getPathSegments().get(1), null);
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            case NOTE_ATT_ID:
                if (selectionArgs != null) {
                    throw new IllegalArgumentException(
                            "can not use selectionArgs when use profile_id");
                }
                count = db.delete(NoteContent.TABLE_ATT, NoteContent.Attachment._ID + "="
                        + uri.getPathSegments().get(1), null);
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            default:
                throw new UnsupportedOperationException();
        }
        return count;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
        int count = 0;
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        switch (sURLMatcher.match(uri)) {
            case NOTE:
                count = db.update(NoteContent.TABLE_NOTE, values, selection, selectionArgs);
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            case NOTE_ID:
                if (selectionArgs != null) {
                    throw new IllegalArgumentException(
                            "can not update by selectionArgs when use profile_id");
                }
                count = db.update(NoteContent.TABLE_NOTE, values, NoteContent.Note._ID + "="
                        + uri.getPathSegments().get(1), null);
                getContext().getContentResolver().notifyChange(uri, null);
                break;
            default:
                throw new UnsupportedOperationException();
        }
        return count;
    }
}
